package org.catspaw.cherubim.persistence.dao.spring.jdbc;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.catspaw.cherubim.persistence.dao.GenericDao;
import org.catspaw.cherubim.persistence.jdbc.PageSqlHelper;

public class GenericSpringJdbcDao<E, P extends Serializable> extends
		BaseSpringJdbcDao<E, P> implements GenericDao<E, P> {

	private String				tableName;
	private String				idFieldName;
	private UpdateSpringJdbcDao	updateSpringJdbcDao;
	private SearchSpringJdbcDao	searchSpringJdbcDao;

	public GenericSpringJdbcDao(PageSqlHelper pageSqlHelper,
			Class<E> entityClass) {
		super(pageSqlHelper);
		setEntityClass(entityClass);
	}

	public GenericSpringJdbcDao(PageSqlHelper pageSqlHelper,
			Class<E> entityClass, Class<P> idClass) {
		super(pageSqlHelper);
		setEntityClass(entityClass);
		setIdClass(idClass);
	}

	@Override
	protected void initDao() {
		if (updateSpringJdbcDao == null) {
			updateSpringJdbcDao = new UpdateSpringJdbcDao();
			updateSpringJdbcDao.setJdbcTemplate(getJdbcTemplate());
		}
		if (searchSpringJdbcDao == null) {
			searchSpringJdbcDao = new SearchSpringJdbcDao(getPageSqlHelper());
			searchSpringJdbcDao.setJdbcTemplate(getJdbcTemplate());
		}
	}

	public Object batch(String sql, Object[][] params, int batchSize) {
		return updateSpringJdbcDao.batch(sql, params, batchSize);
	}

	public Object batch(String sql, Object[][] params) {
		return updateSpringJdbcDao.batch(sql, params);
	}

	public Object update(String sql, Object... params) {
		return updateSpringJdbcDao.update(sql, params);
	}

	public List<?> findList(String sql, int offset, int max,
			Map<String, Object> namedParams, Object[] ordinalParams) {
		return searchSpringJdbcDao.findList(sql, offset, max, namedParams,
										ordinalParams);
	}

	public List<?> findList(String sql, int offset, int max,
			Map<String, Object> params) {
		return searchSpringJdbcDao.findList(sql, offset, max, params);
	}

	public List<?> findList(String sql, int offset, int max, Object... params) {
		return searchSpringJdbcDao.findList(sql, offset, max, params);
	}

	public List<?> findList(String sql, Map<String, Object> params) {
		return searchSpringJdbcDao.findList(sql, params);
	}

	public List<?> findList(String sql, Object... params) {
		return searchSpringJdbcDao.findList(sql, params);
	}

	public Object findOne(String sql, Map<String, Object> params) {
		return searchSpringJdbcDao.findOne(sql, params);
	}

	public Object findOne(String sql, Object... params) {
		return searchSpringJdbcDao.findOne(sql, params);
	}

	@Override
	public String getTableName() {
		return tableName;
	}

	@Override
	protected String getIdFieldName() {
		return idFieldName;
	}

	public UpdateSpringJdbcDao getUpdateSpringJdbcDao() {
		return updateSpringJdbcDao;
	}

	public void setUpdateSpringJdbcDao(UpdateSpringJdbcDao updateSpringJdbcDao) {
		this.updateSpringJdbcDao = updateSpringJdbcDao;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public void setIdFieldName(String idFieldName) {
		this.idFieldName = idFieldName;
	}
}
